<?php


namespace app\common\model;


use app\common\lib\orm\ModelToTable;
use think\facade\Request;

class Model extends \think\Model
{
    public $columns = [];

    const TYPE_VARCHAR = "varchar";
    const TYPE_TEXT = "text";
    const TYPE_TINYINT = "tinyint";
    const TYPE_INT = "int";
    const TYPE_DECIMAL = "decimal";
    const TYPE_TIMESTAMP = "timestamp";

    static function dropTable(){
        $class = static::class;
        $sql = ModelToTable::drop($class);
        return self::execute($sql);
    }
    static function createTable(){
        $class = static::class;
        $sql = ModelToTable::create($class);
        return self::execute($sql);
    }

    /**
     * 简单增加
     * @param $param
     * @return bool
     */
    static function simpleAdd($param){
        $obj = static::create($param);
        $result = $obj->save();
        return $result;
    }

    /**
     * 简单编辑
     * @param $param
     * @return bool
     */
    static function simpleEdit($param){
        if(isset($param["id"])){
            $one = static::where("id",$param["id"])->find();
            if($one){
                $result = $one->save($param);
                return $result;
            }
        }
        return false;
    }
    //简单删除
    static function simpleDelete($argv){
        $item = [
            "query"=>null
        ];
        if(is_callable($argv)){
            $item["query"]=$argv;
        }else{
            $item = array_merge($item,$argv);
        }
        $l_obj = new static();
        if(is_callable($item["query"])){
            call_user_func($item["query"],$l_obj);
        }
        $result = $l_obj->delete();
        return $result;
    }

    /**
     * 简单列表
     * @param $argv
     * @return mixed
     */
    static function simpleList($argv=[]){
        $item = [
            "query"=>null,
            "result"=>null
        ];
        $item = array_merge($item,$argv);
        $l_obj = new static();
        if(is_callable($item["query"])){
            $query = $item["query"];
            $query($l_obj);
        }
        $result = $l_obj->paginate();
        $result_callable = $item["result"];
        if(is_callable($result_callable)){
            $result = $result->toArray();
            if(isset($result["data"])){
                foreach ($result["data"] as $k=>&$row){
                    $result_callable($row);
                    if(empty($row)){
                        unset($result["data"][$k]);
                    }
                }
            }
        }
        return $result;
    }
    /**
     * 通过id获取
     * @param $id
     * @return array|null|\PDOStatement|string|\think\Model
     */
    static function getById($id){
        $one = static::where("id",$id)->limit(1)->find();
        return $one;
    }

    /**
     * @param $cond
     * @return string|\think\Model|self
     */
    static function simpleOne($cond=[]){
        $one = static::where($cond)->limit(1)->find();
        return $one;
    }
    /**
     * @param $cond
     * @return array|\PDOStatement|string|\think\Collection
     */
    static function simpleAll($cond = []){
        $one = static::where($cond)->select();
        return $one;
    }
}